package cn.com.joycode.nimble.admin.security.configuration;

import cn.com.joycode.nimble.admin.security.authentication.NimbleAdminAuthenticationManager;
import cn.com.joycode.nimble.admin.security.authentication.NimbleAdminAuthenticationProvider;
import cn.com.joycode.nimble.admin.security.filter.NimbleAdminAuthenticationProcessingFilter;
import cn.com.joycode.nimble.admin.security.handler.NimbleAdminAuthenticationFailureHandler;
import cn.com.joycode.nimble.admin.security.handler.NimbleAdminAuthenticationSuccessHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class NimbleAdminSecurityConfigurerAdapter extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Resource
    private NimbleAdminAuthenticationManager nimbleAdminAuthenticationManager;

    @Resource
    private NimbleAdminAuthenticationSuccessHandler nimbleAdminAuthenticationSuccessHandler;

    @Resource
    private NimbleAdminAuthenticationFailureHandler nimbleAdminAuthenticationFailureHandler;

    @Resource
    private NimbleAdminAuthenticationProvider nimbleAdminAuthenticationProvider;

    @Override
    public void configure(HttpSecurity builder) throws Exception {
        log.info("注入安全管理器:{}", this);
        NimbleAdminAuthenticationProcessingFilter filter = new NimbleAdminAuthenticationProcessingFilter();
        filter.setAuthenticationManager(nimbleAdminAuthenticationManager);
        filter.setAuthenticationSuccessHandler(nimbleAdminAuthenticationSuccessHandler);
        filter.setAuthenticationFailureHandler(nimbleAdminAuthenticationFailureHandler);
        builder.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class);
        //builder.authenticationProvider(nimbleAdminAuthenticationProvider);
    }
}